package com.lagou.user.controller;

import com.lagou.user.pojo.User;
import com.lagou.user.remote.CodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/user")
public class UserController {
    //key为user:{email}
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    @Autowired
    private CodeService codeService;

    @GetMapping("/register/{email}/{password}/{code}")
    public int register(@PathVariable String email,@PathVariable String password, @PathVariable String code){
        //注册接⼝，true成功，false失败
        int status = codeService.validate(email, code);
        //0正确1错误2超时
        if(status == 0){
            User user = new User();
            user.setEmail(email);
            user.setPassword(password);

            redisTemplate.opsForValue().set("user:"+email,user,120,TimeUnit.MINUTES);
            return 0;
        }else{
            return status;
        }
    }

    @GetMapping("/isRegistered/{email}")
    public boolean isRegister(@PathVariable String email){
        //是否已注册，根据邮箱判断,true代表已经注册过，
        //false代表尚未注册
        Object obj = redisTemplate.opsForValue().get("user:" + email);
        if(obj == null){
            return false;
        }
        return true;
    }

    @GetMapping("/login/{email}/{password}")
    public String login(@PathVariable String email, @PathVariable String password, HttpServletResponse response) throws Exception{
        //登录接⼝，验证⽤户名密码合法性，根据⽤户名和
        //密码⽣成token，token存⼊数据库，并写⼊cookie
        //中，登录成功返回邮箱地址，重定向到欢迎⻚
        User user = (User)redisTemplate.opsForValue().get("user:" + email);
        if(user != null){
            if(password.equals(user.getPassword())){
                //先获取之前的信息
                String uuid = "";
                if(user.getToken() == null){
                    uuid = UUID.randomUUID().toString();
                    user.setToken(uuid);
                    redisTemplate.opsForValue().set("user:"+email,user,1800,TimeUnit.MINUTES);
                    redisTemplate.opsForValue().set(uuid,email,1800,TimeUnit.MINUTES);
                }else {
                    uuid = user.getToken();
                }
                Cookie cookie = new Cookie("token",uuid);
                cookie.setDomain("www.test.com");
                cookie.setMaxAge(60*60);
                cookie.setPath("/");
                response.addCookie(cookie);
                return user.getEmail();
            }
        }
        return "false";
    }

    @GetMapping("/info/{token}")
    public String login(@PathVariable String token){
        //根据token查询⽤户登录邮箱接⼝
        Object obj = redisTemplate.opsForValue().get(token);
        if(obj != null){
            return obj.toString();
        }
        return "";
    }
}
